package com.zbkj.admin.service.impl;

import com.github.pagehelper.PageInfo;
import com.zbkj.admin.filter.TokenComponent;
import com.zbkj.admin.model.vo.MerchantShareDetailResponse;
import com.zbkj.admin.service.AdminShareService;
import com.zbkj.dal.base.enums.BizTypeEnum;
import com.zbkj.dal.base.enums.RecyclePositionEnums;
import com.zbkj.dal.common.exception.CrmebException;
import com.zbkj.dal.entity.admin.SystemAdmin;
import com.zbkj.dal.entity.ba.BizImg;
import com.zbkj.dal.entity.ba.Share;
import com.zbkj.dal.pojo.request.PageParamRequest;
import com.zbkj.dal.pojo.request.ShareQueryRequest;
import com.zbkj.dal.pojo.response.ba.ShareResponse;
import com.zbkj.dal.pojo.vo.LoginUserVo;
import com.zbkj.service.service.BizImgService;
import com.zbkj.service.service.ShareService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class AdminShareServiceImpl implements AdminShareService {

    private final ShareService shareService;
    private final TokenComponent tokenComponent;
    private final BizImgService bizImgService;


    @Override
    public PageInfo<ShareResponse> querySharesPaged(ShareQueryRequest shareQueryRequest, PageParamRequest pageParamRequest) {
        LoginUserVo loginUser = tokenComponent.getLoginUser();
        SystemAdmin admin = loginUser.getUser();
        if (Objects.isNull(admin)) {
            throw new CrmebException("当前用户没有查阅权限");
        }
        return shareService.querySharesPaged(admin.getMerId(), shareQueryRequest, pageParamRequest);
    }


    @Override
    public MerchantShareDetailResponse detail(String orderNo) {
        Share share = shareService.getByOrderNo(orderNo);
        Assert.notNull(share, "供享订单不存在");
        // 获取图片并且进行排序
        Map<RecyclePositionEnums, BizImg> bizImgs = bizImgService.listByBizId(BizTypeEnum.SHARE.getType(), share.getId()).stream()
                .collect(Collectors.toMap(img -> RecyclePositionEnums.valueOf(img.getPosition()), Function.identity()));
        // 商户供享订单详情
        MerchantShareDetailResponse shareDetailResponse = MerchantShareDetailResponse.buildByShare(share, bizImgs);
        // ---------------------------------


        return null;
    }
}
